<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/21-8:22
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 * 前台找资质 搜索接口
 */

namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Repository\Models\Products;
use App\Repository\Models\ProductCategories;
use App\Repository\Models\AttributeValues;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ShopRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use App\Repository\Repositories\Interfaces\ProductCategoriesRepository;
use Illuminate\Http\Request;

class QualifiedController extends BaseController
{

    /**
     * @var ProductsRepository
     */
    protected $shop;
    protected $products_repo;
    protected $productCategories_repo;

    public function __construct(ProductsRepository $products_repo , ProductCategoriesRepository $productCategories_repo ,ShopRepository $shop)
    {
        parent::__construct();

        $this->shop = $shop;
        $this->products_repo = $products_repo;
        $this->productCategories_repo = $productCategories_repo;

        $this->products_repo->scopeQuery(function ($query) {
            return $query->where('product_type', Products::IS_QUALIFIED_TYPE); // 资质商品
            //return $query->where('product_type', Products::IS_NORMAL_TYPE);  // 普通商品
        });

        $this->productCategories_repo->scopeQuery(function ($query) {
            return $query->where('cate_type', ProductCategories::QUALIFIED_CATE);
        });
    }
    /**
     * TODO 获取所有资质商品
     * */
    public function products(){

        $products = $this->products_repo->all(['shop_id']);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $products['data'],true);
    }
    /**
     * TODO 找资质首页 + 搜索 店铺
     * */
    public function show(){
        
        $shops = $this->shop->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops, true);
    }
    /**
     * TODO 获取找资质的分类列表
     * */
    public function getCate(){
        $cates = $this->productCategories_repo->all();
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $cates['data']);
    }
    /**
     *  TODO 查出指定属性的属性值 获取 资质等级
     * */
    public function getCateLevel($attribute_id){
        $catesLevel = AttributeValues::query()->where(['attribute_id'=>$attribute_id])->get()->toArray();
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $catesLevel);
    }
    /**
     *  TODO 测试函数
     * */
    public function t(){
        set_time_limit(0);

        $time1 = microtime(true);
        //$Products = Products::query()->get()->toArray();
        echo date('Y-m-d H:i:s',$time1);
        echo '<br />';

        $Products = $this->products_repo->all(['id','name','product_desc','product_type','created_at','images','seokeywords']);
        //echo $this->products_repo->all(['id'])->count();

        $time2 = microtime(true);
        echo '查询结束时间'.date('Y-m-d H:i:s',$time2);
        echo '<br />';

        foreach ($Products['data'] as $k=>&$v){
            $v['images'] = json_decode($v['images'],1);
            $v['seokeywords'] = array_filter(explode('_',$v['seokeywords']));
            //echo $k;echo "<br />";
        }

        $time3 = microtime(true);
        echo '循环结束时间'.date('Y-m-d H:i:s',$time3);
        echo '<br />';
        echo 'Products Model 查询 & 循环 时间 '.($time3-$time1).'s'.'<br />';
    }
}